// 引入模块
import Vue from "vue";
import VueRouter from "vue-router";
import routes from './routes'


// 解决连续点击search时出现的报错问题
// 1：修改push方法，添加一个函数， 然后给search的点击事件添加push方法，当出现报错时触发函数即可
Vue.use(VueRouter); //使用Vuerouter
const originPush = VueRouter.prototype.push; //将push存储到一个变量
const originRepalce = VueRouter.prototype.replace; //同理

VueRouter.prototype.push = function (location, okCallBack, errCallBack) {
  if (okCallBack === undefined && errCallBack === undefined) {
    // 如果条件满足，说明没有传递这两个参数
    originPush.call(this, location).catch(() => {
      console.log("have bug , you are fool");
    }); //统一处理报错信息，一劳永逸
  } else {
    originPush.call(this, location, okCallBack, errCallBack); //走正常流程
  }
};

// 和push一样
VueRouter.prototype.replace = function (location, okCallBack, errCallBack) {
  if (okCallBack === undefined && errCallBack === undefined) {
    originRepalce.call(this, location).catch(() => {
      console.log.call("replace have err");
    });
  } else {
    originRepalce.call(this, okCallBack, errCallBack);
  }
};

const router = new VueRouter({
  mode: "history", //设置路由样式
  routes,
  scrollBehavior(to,from,savedPosition){
    if(savedPosition){
      return savedPosition
    }else{
      return{x:0,y:0}
    }
  }
});

// 导出路由对象
export default router;
